package com.xiayong.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.xiayong.datahandler.ICityDataHandler;
import com.xiayong.dto.Pager;
import com.xiayong.entity.City;

@Controller
public class CityController {
    private static Logger logger = LoggerFactory.getLogger(CityController.class);
    @Autowired
    private ICityDataHandler cityDataHandler;

    @RequestMapping(value = "/showCityByPage", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody
    String showCityPage(@ModelAttribute("pager") final Pager pager) {
        CityController.logger.info("用户使用分页组建查询City信息, 分页信息: " + pager);
        if (StringUtils.isBlank(pager.getQuery()))
            pager.setQtype(null);
        pager.setTotal(this.cityDataHandler.getCityTotal(pager));
        CityController.logger.info("查询到City信息总数为: " + pager.getTotal());
        List<City> cityList = this.cityDataHandler.getCityByPager(pager);
        CityController.logger.info("分页查询City信息完毕本次查询到第" + pager.getPage() + "页, " + cityList.size() + "条数据.");
        pager.setCells((Serializable) cityList);
        Map<String, Serializable> jsonMap = new HashMap<String, Serializable>();
        jsonMap.put("total", pager.getTotal());
        jsonMap.put("page", pager.getPage());
        jsonMap.put("rows", pager.getCells());

        return JSON.toJSONString(jsonMap);
    }
}